package cn.xfdzx.android.apps.shop.presenter;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopSearchBeen;
import cn.xfdzx.android.apps.shop.contract.ShopSearchContract;
import cn.xfdzx.android.apps.shop.model.ShopSearchModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class ShopSearchPresenter extends BasePresenter<ShopSearchContract.View> implements ShopSearchContract.Presenter {

    private ShopSearchContract.Model model;

    public ShopSearchPresenter() {
        model = new ShopSearchModel();
    }


    @Override
    public void initSearchData(boolean isRefresh, boolean isLoading,String lng, String lat, String sid, String page, String limit, String index_status, String is_store, String store_id, String keyword, String priceOrder, String salesOrder, String news) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.initSearchData(lng,lat,sid, page, limit, index_status, is_store, store_id, keyword, priceOrder, salesOrder, news).compose(RxScheduler.<ShopSearchBeen>Flo_io_main())
                .as(mView.<ShopSearchBeen>bindAutoDispose()).subscribe(new Consumer<ShopSearchBeen>() {
            @Override
            public void accept(ShopSearchBeen baseObjectBean) throws Exception {
                mView.onInitSearchSuccess(baseObjectBean);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void confirm(String  cartId,String  user_curent_lng,String  user_curent_lat) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.confirm(cartId,user_curent_lng,user_curent_lat)
                .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onConfirmSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }





    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void shopCarList(String store_id, String index_status) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.shopCarList(store_id,index_status)
                .compose(RxScheduler.<ShopCarListBeen>Flo_io_main())
                .as(mView.<ShopCarListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopCarListBeen>() {
                    @Override
                    public void accept(ShopCarListBeen bean) throws Exception {
                        mView.onShopCarListSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void updateCarNum(String id,int number,int one_position,int position) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.updateCarNum(id,number+"")
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onUpdateCarNumSuccess(bean,number,one_position,position);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }
    @Override
    public void removeGood(String ids,String good_id,int position,boolean isAll) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.removeGood(ids)
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onRemoveGoodSuccess(bean,good_id,position,isAll);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }


}
